package dao.concret;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import dao.AbstractTable;
import dao.DAOList;
import dao.tables.SPECIALITE;

public class SpecialiteDAO extends DAOList<SPECIALITE> {

    @Override
    public SPECIALITE find(long id) {
	SPECIALITE spec = new SPECIALITE();
	try {
	    Statement stmt = this.connect.createStatement(
		    ResultSet.TYPE_SCROLL_INSENSITIVE,
		    ResultSet.CONCUR_UPDATABLE);
	    ResultSet result = stmt
		    .executeQuery("SELECT * FROM redline.SPECIALITE WHERE IDSPECIALITE = "
			    + id);
	    if (result.first())
		spec = new SPECIALITE(id, result.getString("SPECIALITE"));
	    stmt.close();
	    result.close();
	    stmt = null;
	    result = null;
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
	return spec;
    }

    @Override
    public SPECIALITE create(AbstractTable obj) {
	return null;
    }

    @Override
    public SPECIALITE update(AbstractTable obj) {
	try {
	    Statement stmt = this.connect.createStatement();
	    stmt.executeUpdate("UPDATE redline.SPECIALITE SET SPECIALITE = '"
		    + obj.getValue() + "' WHERE IDSPECIALITE = " + obj.getId());
	    obj = this.find(obj.getId());
	    stmt.close();
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
	return (SPECIALITE) obj;
    }

    @Override
    public SPECIALITE delete(SPECIALITE obj) {
	return null;
    }

    @Override
    public SPECIALITE[] list() {
	ArrayList<SPECIALITE> list = new ArrayList<SPECIALITE>();
	try {
	    Statement stmt = this.connect.createStatement(
		    ResultSet.TYPE_SCROLL_INSENSITIVE,
		    ResultSet.CONCUR_UPDATABLE);
	    ResultSet result = stmt
		    .executeQuery("SELECT IDSPECIALITE FROM redline.SPECIALITE");
	    while (result.next())
		list.add(this.find(result.getLong("IDSPECIALITE")));
	    stmt.close();
	    result.close();
	    stmt = null;
	    result = null;
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
	return (SPECIALITE[]) list.toArray(new SPECIALITE[list.size()]);
    }

    @Override
    public SPECIALITE[] list(long id) {
	ArrayList<SPECIALITE> list = new ArrayList<SPECIALITE>();
	try {
	    Statement stmt = this.connect.createStatement(
		    ResultSet.TYPE_SCROLL_INSENSITIVE,
		    ResultSet.CONCUR_UPDATABLE);
	    ResultSet result = stmt
		    .executeQuery("SELECT IDSPECIALITE FROM redline.SPECIALISER WHERE IDPERSONNE = "
			    + id);
	    while (result.next())
		list.add(this.find(result.getLong("IDSPECIALITE")));
	    result.close();
	    stmt.close();
	    stmt = null;
	    result = null;
	} catch (SQLException sqle) {
	    sqle.printStackTrace();
	}
	return (SPECIALITE[]) list.toArray(new SPECIALITE[list.size()]);
    }
}
